package com.xuexiang.xui_lib.component.button.shadowbutton;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import com.xuexiang.xui_lib.ResourceTable;
import com.xuexiang.xui_lib.util.AttrValue;

/**
 * 可设置阴影效果的ImageView
 *
 * @author xuexiang
 * @since 2019/1/14 下午10:09
 */
public class ShadowImageView extends Image
    implements Component.DrawTask, Component.EstimateSizeListener, Component.TouchEventListener {
    private int mWidth;
    private int mHeight;
    private int mPaintAlpha = 48;

    private Color mPressedColor;
    private Paint mPaint;
    private int mShapeType;
    private int mRadius;

    private RectFloat mRectF;

    /*public ShadowImageView(Context context) {
        super(context);
        init(context, null);
    }*/

    public ShadowImageView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet);
    }

    public ShadowImageView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    private void init(final Context context, final AttrSet attrSet) {
        mPressedColor = AttrValue.get(attrSet, "sb_color_pressed",
            new Color(context.getColor(ResourceTable.Color_default_shadow_button_color_pressed)));
        mPaintAlpha = AttrValue.get(attrSet, "sb_alpha_pressed", mPaintAlpha);
        mShapeType = AttrValue.get(attrSet, "sb_shape_type", 1);
        mRadius = AttrValue.getDimension(attrSet, "sb_radius", AttrHelper.vp2px(2, context));
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(mPressedColor);
        mPaint.setAlpha(0);
        mPaint.setAntiAlias(true);
        this.setClickable(true);
        addDrawTask(this);
        setEstimateSizeListener(this);
        setTouchEventListener(this);
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        mWidth = Component.EstimateSpec.getSize(i);
        mHeight = Component.EstimateSpec.getSize(i1);
        mRectF = new RectFloat(0, 0, mWidth, mHeight);
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mPaint == null) {
            return;
        }
        if (mShapeType == 0) {
            canvas.drawCircle(mWidth / 2F, mHeight / 2F, mWidth / 2.1038f, mPaint);
        } else {
            canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                mPaint.setAlpha(mPaintAlpha / 255f);
                invalidate();
                break;
            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_UP:
                mPaint.setAlpha(0);
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }

    public Color getPressedColor() {
        return mPressedColor;
    }

    /**
     * Set the pressed color.
     *
     * @param pressedColor pressed color
     */
    public void setPressedColor(Color pressedColor) {
        mPaint.setColor(pressedColor);
        invalidate();
    }
}
